package cnm.dao.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cnm.dao.ProgramDAO;
import cnm.model.Program;

import java.util.List;

public class ProgramHibernateDAO extends GenericHibernateDAO<Program> implements ProgramDAO
{
	public ProgramHibernateDAO()
	{
		super(Program.class);
	}

	public Long getCount()
	{
		return (Long) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select count(*) from Program p");							
				return query.uniqueResult();
			}
		}, true);
	}	
	
	@SuppressWarnings("deprecation")
	public Long getCountByKeyword(final String keyword)
	{
		return (Long) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				String queryStr = "select count(*) from Program p where" +
					" p.programName like :name or p.programDesc like :desc or p.column.columnName like:column";
				Query query = session.createQuery(queryStr)
						.setString("name", "%"+keyword+"%")
						.setString("desc", "%"+keyword+"%")
						.setString("column", "%"+keyword+"%");					
				return query.uniqueResult();
			}
		}, true);
	}	

	@SuppressWarnings("deprecation")
	public Long getCountByColumn(final String columnUuid)
	{
		return (Long) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				String queryStr = "select count(*) from Program p where" +
					" p.column.columnUuid like :columnUuid";
				Query query = session.createQuery(queryStr)
						.setString("columnUuid", "%"+columnUuid+"%");			
				return query.uniqueResult();
			}
		}, true);
	}		
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<Program> findByKeyword(final String keyword, final int firstResult, final int maxResults)
	{
		return (List<Program>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				String queryStr = "from Program p " +
						"where p.programName like :name or " +
						"p.programDesc like :desc or " +
						"p.column.columnName like :column " +
						"order by p.programDate desc";
				Query query = session.createQuery(queryStr)
						.setString("name", "%"+keyword+"%")
						.setString("desc", "%"+keyword+"%")	
						.setString("column", "%"+keyword+"%");		
				if (firstResult >= 0)
					query.setFirstResult(firstResult);
				if (maxResults > 0)
					query.setMaxResults(maxResults);	
				return query.list();
			}
		}, true);
	}	
		
	@SuppressWarnings({ "unchecked", "deprecation" })
	public List<Program> findByColumn(final String columnUuid, final int firstResult, final int maxResults)
	{
		return (List<Program>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				String queryStr = "from Program p " +
						"where p.column.columnUuid like :columnUuid " +
						"order by p.programDate desc";
				Query query = session.createQuery(queryStr)
						.setString("columnUuid", "%"+columnUuid+"%");		
				if (firstResult >= 0)
					query.setFirstResult(firstResult);
				if (maxResults > 0)
					query.setMaxResults(maxResults);	
				return query.list();
			}
		}, true);
	}		
}
